<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript">
$(function() {
	
	var urlPrefix = "${pageContext.request.contextPath}/api/";
	var newsNoticeAPI = new NewsNoticeAPI({urlPrefix : urlPrefix});
	var searchCondition = {};
	searchCondition.title = '';
	searchCondition.category = '';
	searchCondition.valid_start_date_from = '';
	searchCondition.valid_start_date_to = '';
	searchCondition.valid_end_date_from = '';
	searchCondition.valid_end_date_to = '';
	searchCondition.time_limit = '';
	var ueditor;
	
	//for debug
	try{throw ''} catch(e){}
	
	$('#news_notice_grid').datagrid({
		url: '${pageContext.request.contextPath}/api/NewsNoticeSearchAPI.json',
	    columns: [[
            {field:'title', title:'标题', width:300, align:'left', halign:'center', resizable:false},
            {field:'category_name', title:'类别', width:100, align:'center', halign:'center', resizable:false},
            {field:'tender_title', title:'招标名称', width:300, align:'left', halign:'center', resizable:false},
            {field:'valid_start_date', title:'有效开始日', width:100, align:'center', halign:'center', resizable:false},
            {field:'valid_end_date', title:'有效结束日', width:100, align:'center', halign:'center', resizable:false},
            {field:'opt', title:'操作', width:130, align:'center', halign:'center', resizable:false,
                formatter: function(value, row, index) {
                    return '<a id="btn_edit" class="button_edit" href="#">编辑</a>&nbsp;<a id="btn_delete" class="button_delete" href="#">删除</a>';
                }
            }
        ]],
        striped: true,
        rownumbers: true,
        singleSelect: true,
        autoRowHeight: false,
        idField: 'news_notice_id',
        pagination: true,
        pageSize: 20,
        pagePosition: 'top',
        pageList: [20],
        loadFilter: function(data) {
        	var datax = {};
        	if (data.total && data.total > 0) {
    			datax.total = data.total;
    			datax.rows = data.news_notice;
        	} else {
        		datax.total = 0;
    			datax.rows = new Array();
        	}
        	return datax;
        },
        onLoadSuccess: function(data) {
            var pager = $('#news_notice_grid').datagrid('getPager');
            pager.pagination({
                buttons:[{
                    iconCls: 'icon-add',
                    text: '追加',
                    handler: function() {
                        showNewsNoticeEditWin();
                    }
                },
                {
                    iconCls: 'icon-search',
                    text: '条件检索',
                    handler: function() {
                        showNewsNoticeConditionWin();
                    }
                }]
            });
            
            if (data.total > 0) {
            	$('#news_notice_list .button_edit').linkbutton({
                    iconCls: 'icon-edit',
                    onClick: function() {
                        var index = $(this).index('#news_notice_list .button_edit');
                        var newsNoticeData = $('#news_notice_list #news_notice_grid').datagrid('getData').rows[index];
                        showNewsNoticeEditWin(newsNoticeData);
                    }
                });
                
                $('#news_notice_list .button_delete').linkbutton({
                    iconCls: 'icon-delete',
                    onClick: function() {
                        var index = $(this).index('#news_notice_list .button_delete');
                        var newsNoticeData = $('#news_notice_list #news_notice_grid').datagrid('getData').rows[index];
                        $.messager.confirm('<s:text name="title.confirm" />', '您确定要删除新闻通告【' + newsNoticeData.title + '】吗？', function(r) {
                        	if (r == true) {
                        	    var paramData = {};
                        	    paramData.news_notice_id = newsNoticeData.news_notice_id;
                        	    newsNoticeAPI.remove({
	                    	        onSuccess : function(data) {
	                    	            if (data && data.result) {
	                    	                $.messager.alert('<s:text name="title.notification" />', $.format('<s:text name="messages.notification.success" />', '新闻通告删除'));
                            	            $('#news_notice_list #news_notice_grid').datagrid('reload');
	                    	            }
	                    	        },
	                    	        onAPIError : function(status, errors) {
	                    	            $.messager.alert('<s:text name="title.error" />', $.format('<s:text name="messages.notification.fail" />', '新闻通告删除'));
	                    	        },
	                    	        onServerError : function(jqXHR, textStatus, errorThrown) {
	                    	            $.messager.alert('<s:text name="title.error" />', $.format('<s:text name="messages.notification.fail" />', '新闻通告删除'));
	                    	        }
	                    	    },
	                    	    {
	                    	        param: JSON.stringify(paramData)
	                    	    });
                        	}
                        });
                    }
                });
                
                $('#news_notice_list #news_notice_grid').datagrid('fixRowHeight');
                
                $.each(data.rows, function(index, row) {
                    if (row.category == 1) {
                        $('#news_notice_list .button_edit').eq(index).linkbutton('enable');
                        $('#news_notice_list .button_delete').eq(index).linkbutton('enable');
                    } else {
                        $('#news_notice_list .button_edit').eq(index).linkbutton('disable');
                        $('#news_notice_list .button_delete').eq(index).linkbutton('disable');
                    }
                });
            }
        },
        onSelect: function(index, row) {
            $("#news_notice_grid").datagrid('unselectRow', index);
        }
	});
	
	function showNewsNoticeEditWin(newsNoticeData) {
	    $('#news_notice_win_modify').window({
            onOpen: function() {
                $('#news_notice_win_modify #title').textbox({
                    required: true,
                    value: newsNoticeData ? newsNoticeData.title : '',
                    validType: {
            			length: [4, 30]
            		}
                });
                
                $.codeNameCombo({
                    urlPrefix: urlPrefix,
                    target: $('#news_notice_win_modify #category'),
                    typeId: '11',
                    required: true,
                    editable: false,
                    readonly: true,
                    missingMessage: $.fn.validatebox.defaults.missingMessage,
                    errorTitle: '<s:text name="title.error" />',
                    errorMessage: '数据取得失败',
                    blank: false,
                    value: newsNoticeData ? newsNoticeData.category : 1
                });
                
                var today = (new Date()).format('yyyy-MM-dd');
                $('#news_notice_win_modify #valid_start_date').datebox({
                    height: 28,
                    width: 100,
            		editable: false,
            		required: true,
            		value: newsNoticeData ? newsNoticeData.valid_start_date : today,
      	    		onSelect: function(date) {
      	    		    if (date.format('yyyy-MM-dd') > $('#news_notice_win_modify #valid_end_date').datebox('getValue')) {
      	    		        $('#news_notice_win_modify #valid_end_date').datebox('setValue', date.format('yyyy-MM-dd'));
      	    		    } else if (date.format('yyyy-MM-dd') < today) {
      	    		        $('#news_notice_win_modify #valid_start_date').datebox('setValue', '').datebox('showPanel');
      	    		    }
      	    		}
                });
                
                $('#news_notice_win_modify #valid_end_date').datebox({
                    height: 28,
                    width: 100,
            		editable: false,
            		required: true,
            		value: newsNoticeData ? newsNoticeData.valid_end_date : today,
      	    		onSelect: function(date) {
      	    		    if (date.format('yyyy-MM-dd') < $('#news_notice_win_modify #valid_start_date').datebox('getValue')) {
      	    		        $('#news_notice_win_modify #valid_end_date').datebox('setValue', '').datebox('showPanel');
      	    		    }
      	    		}
                });
                
                ueditor = UE.getEditor('container', {
            	    autoHeight: false
            	});
                ueditor.ready(function() {
                    if (newsNoticeData) {
                        ueditor.setContent(newsNoticeData.body);
                    }
            	});
                $('#news_notice_win_modify #btn_save').linkbutton({
                    onClick: function() {
                        if ($('#news_notice_edit_form').form('validate') && ueditor.hasContents()) {
                            if (ueditor.getPlainTxt().length > 1000 || ueditor.getContent().length > 65535) {
                                $.messager.alert('<s:text name="title.error" />', '字符数超过最大允许范围！');
                            } else {
                                var paramData = {};
                                paramData.title = $('#news_notice_win_modify #title').textbox('getValue');
                                paramData.category = $('#news_notice_win_modify #category').combobox('getValue');
                                paramData.valid_start_date = $('#news_notice_win_modify #valid_start_date').datebox('getValue');
                                paramData.valid_end_date = $('#news_notice_win_modify #valid_end_date').datebox('getValue');
                                paramData.body = ueditor.getContent();
                                if (newsNoticeData) {
                                    paramData.news_notice_id = newsNoticeData.news_notice_id;
                                    newsNoticeAPI.modify({
		                      	        onSuccess : function(data) {
		                      	        	if (data.status == 0) {
		                      	        	    $.messager.alert('<s:text name="title.notification" />', $.format('<s:text name="messages.notification.success" />', '新闻通告修改'));
		                      	        	    $('#news_notice_grid').datagrid('reload');
		                      	        	    $('#news_notice_win_modify').window('close');
		                      	        	}	        	
		                    	        },
		                    	        onAPIError : function(status, errors) {
		                    	            $.messager.alert('<s:text name="title.notification" />', $.format('<s:text name="messages.notification.fail" />', '新闻通告修改'));
		                    	        },
		                    	        onServerError : function(jqXHR, textStatus, errorThrown) {
		                    	            $.messager.alert('<s:text name="title.notification" />', $.format('<s:text name="messages.notification.fail" />', '新闻通告修改'));
		                    	        }
		                    	    },
		                    	    {
		                    	        param: JSON.stringify(paramData)
		                    	    });
                                } else {
                                    newsNoticeAPI.publish({
		                      	        onSuccess : function(data) {
		                      	        	if (data.status == 0) {
		                      	        	  $.messager.alert('<s:text name="title.notification" />', $.format('<s:text name="messages.notification.success" />', '新闻通告发布'));
		                      	        	    $('#news_notice_grid').datagrid('reload');
		                      	        	    $('#news_notice_win_modify').window('close');
		                      	        	}	        	
		                    	        },
		                    	        onAPIError : function(status, errors) {
		                    	            $.messager.alert('<s:text name="title.notification" />', $.format('<s:text name="messages.notification.fail" />', '新闻通告发布'));
		                    	        },
		                    	        onServerError : function(jqXHR, textStatus, errorThrown) {
		                    	            $.messager.alert('<s:text name="title.notification" />', $.format('<s:text name="messages.notification.fail" />', '新闻通告发布'));
		                    	        }
		                    	    },
		                    	    {
		                    	        param: JSON.stringify(paramData)
		                    	    });
                                }
                            }
                        } else {
                            $.messager.alert('<s:text name="title.error" />', '<s:text name="messages.error.1003" />');
                        }
                    }
                });
                $('#news_notice_win_modify #btn_cancel').linkbutton({
                    onClick: function() {
                        $('#news_notice_win_modify').window('close');
                    }
                });
            },
            onClose: function() {
                ueditor.execCommand('cleardoc');
            }
        });
        $('#news_notice_win_modify').window('open');
	}
	
	function showNewsNoticeConditionWin() {
	    $('#news_notice_win_condition').window({
            onOpen: function() {
                
                $('#news_notice_win_condition #title').textbox({
                    value: searchCondition.title
                });
                
                $.codeNameCombo({
                    urlPrefix: urlPrefix,
                    target: $('#news_notice_win_condition #category'),
                    typeId: '11',
                    required: false,
                    editable: false,
                    missingMessage: $.fn.validatebox.defaults.missingMessage,
                    errorTitle: '<s:text name="title.error" />',
                    errorMessage: '数据取得失败',
                    blank: true,
                    blankText: '全部',
                    value: searchCondition.category
                });
                
                $('#news_notice_win_condition #valid_start_date_from').datebox({
                    height: 28,
                    width: 100,
            		editable: false,
            		value: searchCondition.valid_start_date_from
                });
            
                $('#news_notice_win_condition #valid_start_date_to').datebox({
                    height: 28,
                    width: 100,
            		editable: false,
            		value: searchCondition.valid_start_date_to
                });
            
                $('#news_notice_win_condition #valid_end_date_from').datebox({
                    height: 28,
                    width: 100,		
            		editable: false,
            		value: searchCondition.valid_end_date_from
                });
            
                $('#news_notice_win_condition #valid_end_date_to').datebox({
                    height: 28,
                    width: 100,		
            		editable: false,
            		value: searchCondition.valid_end_date_to
                });
                
                $.codeNameCombo({
                    urlPrefix: urlPrefix,
                    target: $('#news_notice_win_condition #time_limit'),
                    typeId: '12',
                    required: false,
                    editable: false,
                    missingMessage: $.fn.validatebox.defaults.missingMessage,
                    errorTitle: '<s:text name="title.error" />',
                    errorMessage: '数据取得失败',
                    blank: true,
                    blankText: '不限',
                    value: searchCondition.time_limit
                });
                
                $('#news_notice_win_condition #btn_search').linkbutton({
                    onClick: function() {
                        searchCondition.title = $('#news_notice_win_condition #title').textbox('getValue');
                        searchCondition.valid_start_date_from = $('#news_notice_win_condition #valid_start_date_from').datebox('getValue');
                    	searchCondition.valid_start_date_to = $('#news_notice_win_condition #valid_start_date_to').datebox('getValue');
                    	searchCondition.valid_end_date_from = $('#news_notice_win_condition #valid_end_date_from').datebox('getValue');
                    	searchCondition.valid_end_date_to = $('#news_notice_win_condition #valid_end_date_to').datebox('getValue');
                    	searchCondition.category = $('#news_notice_win_condition #category').combobox('getValue');
                    	searchCondition.time_limit = $('#news_notice_win_condition #time_limit').combobox('getValue');
                    	
                        $('#news_notice_grid').datagrid({
                    	    queryParams: {
                                param: JSON.stringify(searchCondition)
                            }
                    	});
                        $('#news_notice_win_condition').window('close');
                    }
                });
            },
            onClose: function() {
                $('#news_notice_win_condition #news_notice_condition_form').form('clear');
            }
        });
        $('#news_notice_win_condition').window('open');
	}
	
});
</script>
<div id="news_notice_list">
  <table id="news_notice_grid"></table>
</div>
<div id="news_notice_win_condition" class="easyui-window" title="检索条件" data-options="modal:true, closed:true, collapsible:false, minimizable:false, maximizable:false, resizable:false, iconCls:'icon-search', shadow: false" style="width:460px; height: auto; padding: 10px;">
  <div style="width: 100%; height: auto;">
    <form id="news_notice_condition_form">
      <p style="margin-top: 5px;">
        <span>标题：</span>
        <input type="text" id="title" style="width: 320px; height: 28px;">
      </p>
      <p style="margin-top: 5px;">
        <span>有效开始日：&nbsp;&nbsp;&nbsp;&nbsp;从&nbsp;</span>
        <input type="text" id="valid_start_date_from">&nbsp;开始，到&nbsp;<input type="text" id="valid_start_date_to">&nbsp;结束
      </p>
      <p style="margin-top: 5px;">
        <span>有效结束日：&nbsp;&nbsp;&nbsp;&nbsp;从&nbsp;</span>
        <input type="text" id="valid_end_date_from">&nbsp;开始，到&nbsp;<input type="text" id="valid_end_date_to">&nbsp;结束
      </p>
      <p style="margin-top: 5px;">
        <span>类别：</span>
        <input id="category" style="width: 100px; height: 28px;" />
        <span>发布时间：</span>
        <input id="time_limit" style="width: 100px; height: 28px;" />
      </p>
    </form>
  </div>
  <div style="text-align: right; margin-top: 10px; margin-right: 10px;">
    <a id="btn_search" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
  </div>
</div>
<div id="news_notice_win_modify" class="easyui-window" title="新闻通告" data-options="modal:true, closed:true, collapsible:false, minimizable:false, maximizable:false, resizable:false, iconCls:'icon-edit', shadow: false" style="width:996px; height: 725px;">
  <form id="news_notice_edit_form">
    <div style="width: 100%; height: auto;">
      <p style="margin-top: 5px;">
        <span>标题：</span>
        <input type="text" id="title" style="width: 600px; height: 28px;">
      </p>
      <p style="margin-top: 5px;">
        <span>类别：</span>
        <input id="category" style="width: 100px; height: 28px;" />&nbsp;&nbsp;
        <span>有效开始日：</span>
        <input type="text" id="valid_start_date">&nbsp;&nbsp;
        <span>有效结束日：</span>
        <input type="text" id="valid_end_date">
      </p>
      <p style="margin-top: 5px;">
        <span>内容：</span>
        <script id="container" type="text/plain"></script>
      </p>
    </div>
    <div style="text-align: right; margin-top: 10px; margin-right: 10px;">
      <a id="btn_save" class="easyui-linkbutton" data-options="iconCls:'icon-save'">保存</a>
      <a id="btn_cancel" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">取消</a>
    </div>
  </form>
</div>